package leetcode.code0014;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution476 implements IDebug {
	public int findComplement(int num) {
		int cur = 0;// 当前攒的数, 缓存
		int pos = 0;// 处理pos+1位
		int ans = 0;// 答案
		while (num > 0) {// 还有数就做
			int last = num & 1;// 最后一位
			if (last == 1) {
				ans |= cur;// 之前的cur都是有效数字, 添加到结果中
				cur = 0;// 再攒
			} else {
				last = 1 << pos;// 记下0变的1
				cur |= last;// 添加到当前攒的数中
			}
			pos++;
			num >>>= 1;
		}
		return ans;
	}

	@Override
	public void debug1() {
		int num = 5;
		int e = 2;
		int r = this.findComplement(num);
		HelpDebug.compare(e, r);
	}

	@Override
	public void debug2() {
		int num = 1;
		int e = 0;
		int r = this.findComplement(num);
		HelpDebug.compare(e, r);
	}

	@Override
	public void debug3() {

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution476 so = new Solution476();
		so.debug1();
		so.debug2();
		int num = 19;
		System.out.println(Integer.toBinaryString(num));

	}

}
